package game.screen;

import game.Constants;
import game.Event;
import game.Event.EventType;
import game.Person;
import game.Person.Gender;
import game.manager.EventManager;
import game.manager.InputManager;
import game.manager.PersonManager;
import game.manager.ScreenManager;
import game.screen.option.BackOption;
import game.screen.option.ForwardOption;
import game.screen.option.ScreenOption;
import game.util.InterestUtil;
import game.util.TraitUtil;
import game.util.InterestUtil.Interest;
import game.util.TraitUtil.Trait;

import java.util.Collection;
import java.util.List;
import java.util.Vector;

public class CharacterBuilderScreen extends Screen {

	@Override
	protected List<ScreenOption> getOptions() {
		Vector<ScreenOption> options = new Vector<ScreenOption>(3);
		options.add(new ScreenOption(Constants.SET_FIRST_NAME) {

			@Override
			public boolean isEnabled() {
				return true;
			}

			@Override
			public void select() {
				final Runnable runnable = new Runnable() {
					@Override
					public void run() {
						final Person user = PersonManager.getManager()
								.getUser();
						String firstName = InputManager.getManager()
								.getStringInput("Enter a first name ");
						user.setFirstName(firstName);
					}
				};
				runnable.run();
			}

		});
		options.add(new ScreenOption(Constants.SET_LAST_NAME) {

			@Override
			public boolean isEnabled() {
				return true;
			}

			@Override
			public void select() {
				final Runnable runnable = new Runnable() {
					@Override
					public void run() {
						final Person user = PersonManager.getManager()
								.getUser();
						String lastName = InputManager.getManager()
								.getStringInput("Enter a last name ");
						user.setLastName(lastName);
					}
				};
				runnable.run();
			}

		});
		options.add(new ScreenOption(Constants.SET_GENDER) {

			@Override
			public boolean isEnabled() {
				return true;
			}

			@Override
			public void select() {
				final Runnable runnable = new Runnable() {
					@Override
					public void run() {
						final Person user = PersonManager.getManager()
								.getUser();
						Gender gender;
						while ((gender = Gender
								.fromString(InputManager.getManager()
										.getStringInput("Enter a gender "))) == null)
							;
						user.setGender(gender);
					}
				};
				runnable.run();
			}

		});
		options.add(new ScreenOption(Constants.CONFIRM_OK) {

			@Override
			public boolean isEnabled() {
				final Person user = PersonManager.getManager().getUser();

				return (!user.getFirstName().isEmpty()
						&& !user.getLastName().isEmpty() && user.getGender() != null);
			}

			@Override
			public void select() {
				ScreenManager.getManager().activateScreen(new MenuScreen(),
						false);
				EventManager.getManager().sendEvent(
						new Event(EventType.GAME_STARTED));
			}

		});
		if (Constants.DEBUG) {
			options.add(new ScreenOption(Constants.DEBUG_OPTION) {

				@Override
				public boolean isEnabled() {
					return true;
				}

				@Override
				public void select() {
					final Person user = PersonManager.getManager().getUser();
					user.setFirstName(Constants.DEBUG_FIRST_NAME);
					user.setLastName(Constants.DEBUG_LAST_NAME);
					user.setGender(Constants.DEBUG_GENDER);

					final InterestUtil interestGenerator = new InterestUtil();
					final Collection<Interest> interests = interestGenerator
							.getRandomInterests();
					user.setInterests(interests);

					final TraitUtil traitGenerator = new TraitUtil();
					final Collection<Trait> traits = traitGenerator.getRandomTraits();
					user.setTraits(traits);

					EventManager.getManager().sendEvent(
							new Event(EventType.GAME_STARTED));
					ScreenManager.getManager().activateScreen(new MenuScreen(),
							false);
				}

			});
		}
		options.add(new BackOption());
		options.add(new ForwardOption());
		return options;
	}

	@Override
	public String getTitle() {
		return Constants.CHARACTER_BUILDER_TITLE;
	}

	@Override
	public void draw() {
	}
}
